home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / rhythmbox / plugins / lyrics / AstrawebParser.py < prev    next >
Encoding:
Python Source  |  2009-04-07  |  3.4 KB  |  94 lines

  1. # -*- Mode: python; coding: utf-8; tab-width: 8; indent-tabs-mode: t; -*-
  2. #
  3. # Copyright (C) 2007 James Livingston
  4. # Copyright (C) 2007 Sirio Bola√±os Puchet
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2, or (at your option)
  9. # any later version.
  10. #
  11. # The Rhythmbox authors hereby grant permission for non-GPL compatible
  12. # GStreamer plugins to be used and distributed together with GStreamer
  13. # and Rhythmbox. This permission is above and beyond the permissions granted
  14. # by the GPL license by which Rhythmbox is covered. If you modify this code
  15. # you may extend this exception to your version of the code, but you are not
  16. # obligated to do so. If you do not wish to do so, delete this exception
  17. # statement from your version.
  18. #
  19. # This program is distributed in the hope that it will be useful,
  20. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22. # GNU General Public License for more details.
  23. #
  24. # You should have received a copy of the GNU General Public License
  25. # along with this program; if not, write to the Free Software
  26. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
  27.  
  28. import urllib
  29. import re
  30. import rb
  31.  
  32. class AstrawebParser (object):
  33.     def __init__(self, artist, title):
  34.         self.artist = artist
  35.         self.title = title
  36.         
  37.     def search(self, callback, *data):
  38.         wartist = re.sub('%20', '+', urllib.quote(self.artist))
  39.         wtitle = re.sub('%20', '+', urllib.quote(self.title))
  40.  
  41.         wurl = 'http://search.lyrics.astraweb.com/?word=%s+%s' % (wartist, wtitle)
  42.  
  43.         loader = rb.Loader()
  44.         loader.get_url (wurl, self.got_results, callback, *data)
  45.  
  46.     def got_results (self, result, callback, *data):
  47.         if result is None:
  48.             callback (None, *data)
  49.             return
  50.  
  51.         results = re.sub('\n', '', re.sub('\r', '', result))
  52.  
  53.         if re.search('(<tr><td bgcolor="#BBBBBB".*)(More Songs >)', results) is not None:
  54.             body = re.split('(<tr><td bgcolor="#BBBBBB".*)(More Songs >)', results)[1]
  55.             entries = re.split('<tr><td bgcolor="#BBBBBB"', body)
  56.             entries.pop(0)
  57.             print "found %d entries; looking for [%s,%s]" % (len(entries), self.title.lower().strip(), self.artist.lower().strip())
  58.             for entry in entries:
  59.                 url = re.split('(\/display[^"]*)', entry)[1]
  60.                 artist = re.split('(Artist:.*html">)([^<]*)', entry)[2]
  61.                 title = re.split('(\/display[^>]*)([^<]*)', entry)[2][1:]
  62.  
  63.                 print "checking [%s,%s]" % (title.lower().strip(), artist.lower().strip())
  64.                 if title.lower().find(self.title.lower().strip()) != -1:
  65.                     if artist.lower().find(self.artist.lower().strip()) != -1:
  66.                         loader = rb.Loader()
  67.                         loader.get_url ('http://display.lyrics.astraweb.com' + url, self.parse_lyrics, callback, *data)
  68.                         return
  69.                 
  70.                 continue
  71.  
  72.         callback (None, *data)
  73.         return
  74.  
  75.     def parse_lyrics(self, result, callback, *data):
  76.         if result is None:
  77.             callback (None, *data)
  78.             return
  79.  
  80.         result = re.sub('\n', '', re.sub('\r', '', result))
  81.        
  82.         artist_title = re.split('(<title>Lyrics: )([^<]*)', result)[2]
  83.         artist = artist_title.split( " - " )[0]
  84.         title  = artist_title.split( " - " )[1]
  85.         
  86.         title = "%s - %s\n\n" % (artist, title)
  87.         lyrics = re.split('(<font face=arial size=2>)(.*)(<\/font><br></td><td*)', result)[2]
  88.         lyrics = title + lyrics
  89.         lyrics = re.sub('<[Bb][Rr][^>]*>', '\n', lyrics)
  90.         lyrics += "\n\nLyrics provided by lyrics.astraweb.com"
  91.  
  92.         callback (lyrics, *data)
  93.  
  94.